/*******************************************************************************
  * 文件：BuzCtrl.c
  * 作者：djy
  * 版本：v1.0.0
  * 日期：2020-11-28
  * 说明：蜂鸣器控制相关
*******************************************************************************/
#include "BuzCtrl.h"
#include "Hardware_Timer.h"

static BuzCtrl_ts sBuzCtrl;

static const BuzCtrlPara_ts asBuzCtrlPara[] = 
{
    {u16BUZ_KEY_PRESS_ON_MS,    u16BUZ_KEY_PRESS_OFF_MS,    u8BUZ_KEY_PRESS_REPEAT,    eBUZ_VALID  },
    {u16BUZ_DRY_FIRE_ON_MS,     u16BUZ_DRY_FIRE_OFF_MS,     u8BUZ_DRY_FIRE_REPEAT,     eBUZ_DRY_FIRE   },
    {u16BUZ_SENSOR_ERR_ON_MS,   u16BUZ_SENSOR_ERR_OFF_MS,   u8BUZ_SENSOR_ERR_REPEAT,   eBUZ_SENSOR_ERR },
    {u16BUZ_FXQ_ERR_ON_MS,      u16BUZ_FXQ_ERR_OFF_MS,      u8BUZ_FXQ_ERR_REPEAT,      eBUZ_FXQ_ERR    },
    {u16BUZ_VOLTAGE_ERR_ON_MS,  u16BUZ_VOLTAGE_ERR_OFF_MS,  u8BUZ_VOLTAGE_ERR_REPEAT,  eBUZ_VOLTAGE_ERR},
    {u16BUZ_FIRE_OFF_ON_MS,     u16BUZ_FIRE_OFF_OFF_MS,     u8BUZ_FIRE_OFF_REPEAT,     eBUZ_FLAMEOUT   },
    {u16BUZ_TIME_OUT_ON_MS,     u16BUZ_TIME_OUT_OFF_MS,     u8BUZ_TIME_OUT_REPEAT,     eBUZ_TIME_OUT   },
    {u16BUZ_FIRE_FAIL_ON_MS,    u16BUZ_FIRE_FAIL_OFF_MS,    u8BUZ_FIRE_FAIL_REPEAT,    eBUZ_FIRE_FAIL  },
};

static const BuzCtrlPara_ts* BuzCtrl_GetPara(BuzType_te eBuzType)
{
    U8 u8Index;

    for(u8Index = 0;u8Index < ARRAY_SIZE(asBuzCtrlPara);u8Index++)
    {
        if(eBuzType == asBuzCtrlPara[u8Index].eBuzType)
        {
            return &asBuzCtrlPara[u8Index];
        }
    }

    return NULL;
}

/*******************************************************************************
  * 函数名: BuzCtrl_Hook1MS
  * 功  能  ：蜂鸣器控制定时回调
  * 参  数 ：无
  * 返回值：无
  * 说  明 ：为确保定时时间准确，请在硬件定时器中断中直接调用
*******************************************************************************/
void BuzCtrl_Hook1MS(void)
{
    if(sBuzCtrl.bBuzState)
    {
        sBuzCtrl.u32Tick++;

        if(sBuzCtrl.u32Tick >= sBuzCtrl.sBuzCtrlPara.u16BuzOnMs)
        {
            // 蜂鸣器开时间到，关闭蜂鸣器
            Hardware_CH7_SquareWaveStop();
            sBuzCtrl.bBuzState = OFF;
            sBuzCtrl.u32Tick   = 0;
            return;
        }
    }
    else
    {
        if(sBuzCtrl.sBuzCtrlPara.u8RepeatCnt > 1)
        {
            // 需要重复
            sBuzCtrl.u32Tick++;

            if(sBuzCtrl.u32Tick >= sBuzCtrl.sBuzCtrlPara.u16BuzOffMs)
            {
                // 蜂鸣器关时间到，开启蜂鸣器
                Hardware_CH7_SquareWaveStart();
                sBuzCtrl.bBuzState = ON;
                sBuzCtrl.u32Tick   = 0;
                sBuzCtrl.sBuzCtrlPara.u8RepeatCnt--;
                return;
            }
            
        }
        // 蜂鸣器响的次数已到，关闭蜂鸣器
        Hardware_CH7_SquareWaveStop();
    }
}

/*******************************************************************************
  * 函数名: BuzCtrl
  * 功  能  ：蜂鸣器控制
  * 参  数 ：BuzType_te eBuzType：声音类型
  * 返回值：无
  * 说  明 ：无
*******************************************************************************/
void BuzCtrl(BuzType_te eBuzType)
{
   const BuzCtrlPara_ts *psBuzCtrlPara = BuzCtrl_GetPara(eBuzType);
   
   if(psBuzCtrlPara != NULL)
   {
       Hardware_CH7_SquareWaveStop();
       sBuzCtrl.sBuzCtrlPara.eBuzType   = eBuzType;
       sBuzCtrl.sBuzCtrlPara.u16BuzOnMs = psBuzCtrlPara->u16BuzOnMs;
       sBuzCtrl.sBuzCtrlPara.u16BuzOffMs= psBuzCtrlPara->u16BuzOffMs;
       sBuzCtrl.sBuzCtrlPara.u8RepeatCnt= psBuzCtrlPara->u8RepeatCnt;

        if(sBuzCtrl.sBuzCtrlPara.u16BuzOnMs > 0)
        {
            // 开启PWM
            sBuzCtrl.bBuzState = ON;
            Hardware_CH7_SquareWaveStart();
        }
   }
}

void BuzStop(void)
{
	sBuzCtrl.sBuzCtrlPara.eBuzType   = 0;
	sBuzCtrl.sBuzCtrlPara.u16BuzOnMs = 0;
	sBuzCtrl.sBuzCtrlPara.u16BuzOffMs= 0;
	sBuzCtrl.sBuzCtrlPara.u8RepeatCnt= 0;
	// 关闭蜂鸣器
    Hardware_CH7_SquareWaveStop();
}

/*******************************************************************************
  * 函数名: BuzInit
  * 功  能  ：蜂鸣器控制初始化
  * 参  数 ：无
  * 返回值：无
  * 说  明 ：无
*******************************************************************************/
void BuzInit(void)
{
    sBuzCtrl.bBuzState   = OFF;
    sBuzCtrl.u32Tick     = 0;
    sBuzCtrl.sBuzCtrlPara.u16BuzOnMs  = 0;
    sBuzCtrl.sBuzCtrlPara.u16BuzOffMs = 0;
    sBuzCtrl.sBuzCtrlPara.u8RepeatCnt = 0;
}

